#!/bin/bash
#

## chrome is not installed, we use lite version.
export LITE=true

SCRIPT=$(realpath  "$0")
MY_DIR=$(dirname "$SCRIPT")
IPTV_GIT=$MY_DIR/../iptv-api

RESULT_FILE=$IPTV_GIT/output/result.txt
MY_FILE=$MY_DIR/my_source.txt
OUTPUT_FILE=$MY_DIR/source.txt
cd $IPTV_GIT

echo "========= activating python ========="
. ~/conda.init.sh
conda activate iptv
echo "========= cleaning output ========="
rm -f $IPTV_GIT/output/*
echo "========= iptv-api start ========="
pipenv run dev
echo "========= iptv-api end ========="
if [ -f "$RESULT_FILE" ];then
  echo "$RESULT_FILE is generated."
else
  echo "$RESULT_FILE is not found."
  exit 1
fi
cp $RESULT_FILE ${RESULT_FILE}.bk
sed 's/\([^,]*\)-\([^,]*\)/\1\2/' ${RESULT_FILE}.bk > $RESULT_FILE
cat $MY_FILE $RESULT_FILE  > $OUTPUT_FILE
echo "========= done ========="
